package com.syq.web;

import com.syq.bean.Grade;
import com.syq.bean.Student;
import com.syq.service.GradeService;
import com.syq.service.StudentService;
import com.syq.service.impl.GradeServiceImpl;
import com.syq.service.impl.StudentServiceImpl;
import com.syq.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("addStu".equals(method)) {
            addStu(req,resp);
        } else if("delStu".equals(method)) {
            delStu(req,resp);
        } else if("updateStu".equals(method)) {
            updateStu(req,resp);
        } else if("findByStuId".equals(method)) {
            findByStuId(req,resp);
        } else {
            findStuList(req,resp);
        }
    }

    /**
     * 添加学生
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introdction = req.getParameter("introduction");
        //将参数封装到学生对象中
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idnumber);
        student.setProfession(profession);
        student.setIntrodction(introdction);
        //调取service
        StudentService service=new StudentServiceImpl();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        int i = service.insertStu(student);
        if(i>0){
            writer.println("<script>alert('新增成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGradeList'</script>");
        }
    }

    /**
     * 删除学生
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService service = new StudentServiceImpl();
        int i = service.delStu(sid);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet'</script>");
        }
    }

    /**
     * 查询学生列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    protected void findStuList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if(session.getAttribute("user")==null) {
            System.out.println("未登录");
            resp.sendRedirect("/login.jsp");
            return;
        }
        PageUtil pageUtil = new PageUtil();
        //1、摸查
        String stuName = req.getParameter("stuname");
        String stuNo = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //2、分页数据 limit开始位置,显示条数
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        pageUtil.setPageIndex(index);
        StudentServiceImpl studentService = new StudentServiceImpl();


        int pageSize = pageUtil.getPageSize();//每页展示条数
        int sexid = -1;
        if(sex!=null && sex.length()>0) {
            sexid = Integer.parseInt(sex);
        }
        //获取总页数 总条数%每页显示>0？总条数/每页显示+1:总条数/每页显示
        int toatal = studentService.total(stuName,stuNo,sexid);
        pageUtil.setTotal(toatal);
        int totalPages = pageUtil.getTotalPages();



        List<Student> list = studentService.getStudents(stuName,stuNo,sexid,index,pageSize);
        pageUtil.setDataList(list);

        req.setAttribute("stuname",stuName);
        req.setAttribute("stuno",stuNo);
        req.setAttribute("sex",sexid);
//        req.setAttribute("stulist",list);
//        req.setAttribute("pageindex",index);
//        req.setAttribute("pagesize",pageSize);
//        req.setAttribute("totalpages",totalPages);
//        req.setAttribute("total",toatal);
        req.setAttribute("p1",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    /**
     * 更新学生
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
        String stuId = req.getParameter("stuId");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introdction = req.getParameter("introduction");
        //将参数封装到学生对象中
        Student student = new Student();
        student.setStuId(Integer.parseInt(stuId));
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idnumber);
        student.setProfession(profession);
        student.setIntrodction(introdction);

        StudentService service = new StudentServiceImpl();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        int i = service.updateStu(student);
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/Educational/student/studentServlet'</script>");
        }else{
            writer.println("<script>alert('修改失败');location.href='/Educational/student/studentServlet?method=findById&sid="+stuId+"'</script>");
        }
    }
    //主键查询
    protected void findByStuId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService service = new StudentServiceImpl();
        Student student = service.findById(Integer.parseInt(sid));


        //查询年纪列表
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> list = gradeService.getList();
        req.setAttribute("glist",list);
        req.setAttribute("stu",student);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
}
